package com.sanjiahantang.youjiaclient.module.common.presenters;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;


import com.google.gson.Gson;
import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpPresenter;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.config.SPConfig;
import com.sanjiahantang.youjiaclient.module.common.beans.MessageBean;
import com.sanjiahantang.youjiaclient.module.common.beans.MessageUseNoticeBean;
import com.sanjiahantang.youjiaclient.module.common.beans.StringBean;
import com.sanjiahantang.youjiaclient.module.common.models.IUserModel;
import com.sanjiahantang.youjiaclient.module.common.models.impl.UserModel;
import com.sanjiahantang.youjiaclient.module.common.views.MessageView;
import com.sanjiahantang.youjiaclient.module.common.views.impl.WebActivity;
import com.sanjiahantang.youjiaclient.module.me.contract.models.IContractModel;
import com.sanjiahantang.youjiaclient.module.me.contract.models.impl.ContractModel;
import com.sanjiahantang.youjiaclient.module.me.order.beans.RequireOrderBean;
import com.sanjiahantang.youjiaclient.module.me.order.models.IOrderModel;
import com.sanjiahantang.youjiaclient.module.me.order.models.impl.OrderModel;
import com.sanjiahantang.youjiaclient.module.me.order.views.impl.OrderConfirmActivity;
import com.sanjiahantang.youjiaclient.module.me.require.beans.RequireListBean;
import com.sanjiahantang.youjiaclient.utils.RelativeDateFormat;
import com.sanjiahantang.youjiaclient.utils.SPUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import q.rorbin.badgeview.QBadgeView;


/**
 * Created by zy on 2017/9/5.
 */

public class MessagePresenter extends BaseMvpPresenter<MessageView> implements CallBack<MessageBean>{
    private IUserModel userModel;
    private QuickAdapter<MessageBean.Data> quickAdapter;
    private Gson gson;
    private List<MessageBean.Data> dataList;
    private IContractModel contractModel;
    private IOrderModel orderModel;

    public MessagePresenter(){
        userModel = new UserModel();
        gson=new Gson();
        dataList=new ArrayList<>();
        contractModel = new ContractModel();
        orderModel = new OrderModel();
    }
    public void getMessage(Context context) {
        mView.showProgress();
        String customerPhone= SPUtils.get(context, SPConfig.PHONE,"").toString();
        Log.e("参数",customerPhone);
        Map<String,String> params=new HashMap<>();
        params.put("customerPhone",customerPhone);
        params.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        userModel.getUserMessage(context,params,this);
    }

    @Override
    public void onSuccess(MessageBean response, Context context, Object tag) {
        if (mView == null) return;
        mView.hideProgress();
        if(response != null && response.getCode() == RESPONSE_CODE_SUC){
            Log.e("返回",""+response.getData());
            try {
                MessageBean.ListData data = gson.fromJson(response.getData(), MessageBean.ListData.class);
                if (dataList.size() > 0) {
                    dataList.clear();
                }
                dataList.addAll(data.notReadList);
                dataList.addAll(data.readList);
                quickAdapter.clear();
                quickAdapter.addAll(dataList);
            }catch (Exception e){
                e.printStackTrace();
            }
        }else{

        }
    }

    @Override
    public void onFailure(String message) {
        if (mView == null) return;
        mView.hideProgress();
    }

    public List<MessageBean.Data> getDataList(){
        return dataList;
    }
    public ListAdapter getAdapter(Context context) {
        return quickAdapter = new QuickAdapter<MessageBean.Data>(context, R.layout.item_message) {
            @Override
            protected void convert(BaseAdapterHelper helper, MessageBean.Data item) {

                helper.setText(R.id.txtTitle,item.getNoticeTitle());
                TextView txtDetail = helper.getView(R.id.txtDetail);
                txtDetail.setText(Html.fromHtml(item.getNoticeContent()));
                if (item.getCreateTime() != null){
                    try {
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
                        Date date = df.parse(item.getCreateTime().substring(0,"yyyy-MM-dd HH:mm:ss".length()));
                        helper.setText(R.id.txtSendTime, new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }

                String type = item.getNoticeTypeId();
                if("1".equals(type)){
                    helper.setImageResource(R.id.ivType,R.drawable.i_ring_big);
                }else if("2".equals(type)){
                    helper.setImageResource(R.id.ivType,R.drawable.i_ring_big);
                }else if("0".equals(type)){
                    helper.setImageResource(R.id.ivType,R.drawable.i_ring_big);
                }
                ImageView ivType = helper.getView(R.id.ivType);
                if ("1".equals(item.getReadFlg())){
                    helper.setVisible(R.id.ivPoint,false);
//                    new QBadgeView(context).bindTarget(ivType).setBadgeGravity(Gravity.TOP | Gravity.END)
//                            .setBadgeText(" ")
//                            .setGravityOffset(-2,-2, true)
//                            .setBadgeTextSize(8, true);
                }else {
                    helper.setVisible(R.id.ivPoint,true);
                }
            }
        };
    }

    public void goOrderConfirmPage(Context context, final int requireId) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put("requireId",String.valueOf(requireId));
        contractModel.findUserRequireByIdPhone(context,param, new CallBack<RequireListBean>() {
            @Override
            public void onSuccess(final RequireListBean requireListBean, Context context, Object tag) {
                if (requireListBean != null && requireListBean.getCode() == RESPONSE_CODE_SUC){
                    Map<String, String> param = new HashMap<>();
                    param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
                    param.put("requireId", String.valueOf(requireId));
                    orderModel.searchPriceDesignPhoneForRole(context, param, new CallBack<RequireOrderBean>() {
                        @Override
                        public void onSuccess(RequireOrderBean requireOrderBean, Context context, Object tag) {
                            if (requireOrderBean != null && requireOrderBean.getCode() == RESPONSE_CODE_SUC){
                                context.startActivity(new Intent(context, OrderConfirmActivity.class)
                                        .putExtra("requireData",requireListBean.getItem())
                                        .putExtra(KEY_PARAM,requireOrderBean.getData())
                                );
                            }

                        }

                        @Override
                        public void onFailure(String message) {

                        }
                    });
                }else {
                }
            }

            @Override
            public void onFailure(String message) {

            }
        });
    }

    public void findNoticeInfoById(Context context, String destId, BaseView.OnLoadListener listener) {
        Map<String,String> param = new HashMap<>();
        param.put("snId",destId);
        if (mView == null) return;
        mView.showProgress();
        userModel.findNoticeInfoById(context,param, new CallBack<StringBean>() {
            @Override
            public void onSuccess(StringBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if(response != null && RESPONSE_CODE_SUC == response.getCode()){
                    //打开表单详情页面
                    MessageBean.Data data = new Gson().fromJson(response.getData(),MessageBean.Data.class);
                    Intent intent=new Intent(context,WebActivity.class);
                    intent.putExtra(KEY_TYPE,WEB_TYPE_MESSAGE);
                    intent.putExtra(KEY_NOTICE_CONTENT,data.getNoticeContent());
                    context.startActivity(intent);
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
            }
        });
    }
}
